<template>
  <div v-if="visible" class="modal-overlay" @click.self="handleCancel">
    <div class="modal-content">
      <h3>上传图片 ({{ currentIndex + 1 }}/{{ totalImages }})</h3>

      <div class="image-info">
        <p class="info-label">原始路径：</p>
        <p class="info-value">{{ currentImage?.path }}</p>

        <p class="info-label">建议文件名：</p>
        <p class="info-value">{{ suggestedFileName }}</p>
      </div>

      <div class="upload-area">
        <input
          ref="fileInput"
          type="file"
          accept="image/jpeg,image/jpg,image/png,image/gif,image/webp,image/bmp"
          @change="handleFileSelect"
          class="file-input"
        />
        <div class="upload-prompt" @click="triggerFileInput">
          <div v-if="!selectedFile" class="prompt-content">
            <span class="icon">📁</span>
            <p>点击选择图片文件</p>
            <p class="hint">支持 jpg, png, gif, webp, bmp 格式，最大 10MB</p>
          </div>
          <div v-else class="selected-file">
            <span class="icon">✅</span>
            <p class="file-name">{{ selectedFile.name }}</p>
            <p class="file-size">{{ formatFileSize(selectedFile.size) }}</p>
          </div>
        </div>
      </div>

      <div class="modal-actions">
        <button @click="handleSkip" class="modal-btn">跳过</button>
        <button @click="handleCancel" class="modal-btn">取消全部</button>
        <button @click="handleConfirm" class="modal-btn primary" :disabled="!selectedFile">
          确定上传
        </button>
      </div>

      <div v-if="uploading" class="uploading-overlay">
        <div class="spinner"></div>
        <p>上传中...</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  currentImage: {
    type: Object,
    default: null
  },
  currentIndex: {
    type: Number,
    default: 0
  },
  totalImages: {
    type: Number,
    default: 0
  },
  uploading: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['confirm', 'skip', 'cancel'])

const fileInput = ref(null)
const selectedFile = ref(null)

// 计算建议的文件名
const suggestedFileName = computed(() => {
  if (!props.currentImage?.path) return ''
  const parts = props.currentImage.path.split(/[/\\]/)
  return parts[parts.length - 1] || 'image.png'
})

// 监听当前图片变化，重置选中的文件
watch(() => props.currentImage, () => {
  selectedFile.value = null
})

// 触发文件输入
const triggerFileInput = () => {
  if (fileInput.value) {
    fileInput.value.click()
  }
}

// 处理文件选择
const handleFileSelect = (event) => {
  const file = event.target.files[0]

  if (!file) {
    return
  }

  // 验证文件类型
  const validTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp', 'image/bmp']
  if (!validTypes.includes(file.type)) {
    alert('请选择有效的图片文件（jpg, png, gif, webp, bmp）')
    return
  }

  // 验证文件大小（10MB）
  const maxSize = 10 * 1024 * 1024
  if (file.size > maxSize) {
    alert('图片大小不能超过 10MB')
    return
  }

  selectedFile.value = file
}

// 格式化文件大小
const formatFileSize = (bytes) => {
  if (bytes === 0) return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}

// 确定上传
const handleConfirm = () => {
  if (selectedFile.value) {
    emit('confirm', selectedFile.value)
  }
}

// 跳过当前图片
const handleSkip = () => {
  emit('skip')
}

// 取消全部
const handleCancel = () => {
  emit('cancel')
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  animation: fadeIn 0.2s;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-content {
  background: white;
  border-radius: 12px;
  padding: 24px;
  min-width: 500px;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  animation: slideUp 0.3s;
  position: relative;
}

@keyframes slideUp {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.modal-content h3 {
  margin: 0 0 20px 0;
  font-size: 18px;
  color: #333;
  font-weight: 600;
}

.image-info {
  background: #f5f7fa;
  padding: 16px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.info-label {
  font-size: 13px;
  color: #666;
  margin: 0 0 4px 0;
  font-weight: 500;
}

.info-value {
  font-size: 14px;
  color: #333;
  margin: 0 0 12px 0;
  word-break: break-all;
  font-family: 'Courier New', monospace;
}

.info-value:last-child {
  margin-bottom: 0;
}

.upload-area {
  margin-bottom: 20px;
}

.file-input {
  display: none;
}

.upload-prompt {
  border: 2px dashed #d9d9d9;
  border-radius: 8px;
  padding: 40px 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
  background: #fafafa;
}

.upload-prompt:hover {
  border-color: #1890ff;
  background: #f0f7ff;
}

.prompt-content .icon,
.selected-file .icon {
  font-size: 48px;
  display: block;
  margin-bottom: 12px;
}

.prompt-content p {
  margin: 8px 0;
  color: #333;
  font-size: 14px;
}

.hint {
  font-size: 12px !important;
  color: #999 !important;
}

.selected-file {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.file-name {
  font-size: 14px;
  color: #333;
  font-weight: 500;
  margin: 8px 0 4px 0;
}

.file-size {
  font-size: 12px;
  color: #999;
  margin: 0;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.modal-btn {
  padding: 8px 20px;
  font-size: 14px;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  background: white;
  color: #666;
  cursor: pointer;
  transition: all 0.3s;
}

.modal-btn:hover:not(:disabled) {
  border-color: #1890ff;
  color: #1890ff;
}

.modal-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.modal-btn.primary {
  background: #1890ff;
  border-color: #1890ff;
  color: white;
}

.modal-btn.primary:hover:not(:disabled) {
  background: #40a9ff;
  border-color: #40a9ff;
  color: white;
}

.uploading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.95);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  z-index: 10;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f0f0f0;
  border-top-color: #1890ff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 12px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.uploading-overlay p {
  font-size: 14px;
  color: #666;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .modal-content {
    min-width: 90%;
    padding: 20px;
  }

  .modal-actions {
    flex-wrap: wrap;
  }

  .modal-btn {
    flex: 1;
    min-width: 80px;
  }
}
</style>
